package depot;

public class Parcel {
	
	private String parcelID;
	private int day;
	private double weight;
	private String dimension;
	
	/**
	 * Constructor of parcel
	 * @param parcelID
	 * @param day
	 * @param weight
	 * @param dimension
	 * @throws IDFormatException
	 * @throws DimensionFormatException
	 */
	public Parcel(String parcelID, int day, double weight, String dimension) throws IDFormatException, DimensionFormatException{
		
		String firstLetter_ID = parcelID.substring(0, 1);
		
		//check whether the first character of the parcel ID is L or N
		if(firstLetter_ID.equals("L") || firstLetter_ID.equals("N")){
			
			//check if the length of the parcel ID is 4
			if(parcelID.length() == 4){
				
				//check if the last three digits of parcel ID are all digits
				 for (int i=1; i<=parcelID.length()-1; i++){
					 
					if(parcelID.charAt(i)>='0'&& parcelID.charAt(i)<='9'){
							 this.parcelID = parcelID;
				 	}else
						{throw new IDFormatException(parcelID);}
					
				 }
			}else
				{throw new IDFormatException(parcelID);}
		}else
			throw new IDFormatException(parcelID);

		this.day = day;

		this.weight = weight;
		
		//Using "x" to separate dimension
		String parts[] = dimension.split("x");
		
		//Check if dimension has been separated to 3 parts
		if (parts.length == 3){
			
			//check if every part can be transfer to a double type
			for(int index = 0; index < parts.length;index++){
				if(isDouble(parts[index]))
					this.dimension = dimension;
				else
					throw new DimensionFormatException(dimension);
			}
     	}else
     		throw new DimensionFormatException(dimension);		
	}

	/**
	 * This method is used to judge if the selected String can be changed to a double type.
	 * @param string
	 * @return true or false
	 */
	public boolean isDouble(String string){
		try{
			Double.parseDouble(string);
			return true;
		}
		catch(NumberFormatException nfe){
			return false;
		}
	}
	
	/**
	 * Get the value of parcelID
	 * @return parcelID
	 */
	public String getParcelID(){
		return parcelID;
	}
	
	
	/**
	 * Get the value of day
	 * @return day
	 */
	public int getDay(){
		return day;
	}
	
	
	/**
	 * Get the value of weight
	 * @return weight
	 */
	public double getWeight(){
		return weight;
	}
	
	
	/**
	 * Get the value of dimension
	 * @return dimension
	 */
	public String getDimension(){
		return dimension;
	}

}
